import 'package:enterprise_v6_app/app/components/common/custom_no_data.dart';
import 'package:enterprise_v6_app/app/components/common/custom_text_widget.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';

/// 审批流程页面(通用)
class ApprovalProcessWidget extends StatefulWidget {
  const ApprovalProcessWidget({this.nodeList, super.key});

  /// 审批流程节点数据
  final List<ProcessNodesEntity>? nodeList;

  @override
  State<ApprovalProcessWidget> createState() => _ClosureApprovalProcessWidgetState();
}

class _ClosureApprovalProcessWidgetState extends State<ApprovalProcessWidget> {
  /// 展示的节点数据
  final List<ProcessNodesEntity> showNodeList = [];

  @override
  void initState() {
    super.initState();
    // 初始化展示的节点数据
    _initShowData();
  }

  /// 初始化展示数据
  void _initShowData() {
    if (widget.nodeList != null && widget.nodeList!.isNotEmpty) {
      // 去除结束节点
      // showNodeList.addAll(widget.nodeList!.sublist(0, widget.nodeList!.length - 1));
      showNodeList.addAll(widget.nodeList!);
    }
  }

  @override
  Widget build(BuildContext context) {
    if (showNodeList.isEmpty) {
      return const CustomNoData();
    }
    return Container(
      alignment: Alignment.topCenter,
      color: const Color(0xffffffff),
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.w),
      child: SingleChildScrollView(
        child: Column(
            children: showNodeList.asMap().keys.map((index) {
          final ProcessNodesEntity item = showNodeList[index];
          return CustomIntrinsicCircleLine(
            iconSpaceHeight: 0.w,
            iconWidget: _getNodeIconView(index, item),
            showWidget: Container(
              margin: EdgeInsets.only(bottom: 20.w),
              child: _getNodeView(index: index, item: item),
            ),
          );
        }).toList()),
      ),
    );
  }

  /// 获取流程节点图标样式
  Widget _getNodeIconView(int index, ProcessNodesEntity item) {
    //   approvalResult 1通过 2驳回，
    return SvgPicture.asset(
      item.approvalResult == '2' ? AppImages.icon_job_warning : AppImages.icon_process_pass,
      width: 28.w,
      height: 28.w,
    );
  }

  /// 流程节点样式
  Widget _getNodeView({required int index, required ProcessNodesEntity item}) {
    if (item.approvalResult!.isEmpty) {
      return _getStartNodeView(item: item);
    } else {
      return _getProcessNodeView(item: item);
    }
  }

  /// 流程节点开始样式
  Widget _getStartNodeView({required ProcessNodesEntity item}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
      decoration: BoxDecoration(
        color: const Color(0xffffffff),
        border: Border.all(
          color: const Color(0xffDFE5FD),
          width: 1.w,
        ),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Column(
        children: [
          CustomTextWidget.getTextRowView(title: '${item.nodeName}', value: item.createBy!),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '提交时间', value: item.createDate!),
        ],
      ),
    );
  }

  /// 流程节点样式
  Widget _getProcessNodeView({required ProcessNodesEntity item}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 14.w, vertical: 16.w),
      decoration: BoxDecoration(
        color: const Color(0xffffffff),
        border: Border.all(
          color: const Color(0xffDFE5FD),
          width: 1.w,
        ),
        borderRadius: BorderRadius.circular(4.r),
      ),
      child: Column(
        children: [
          CustomTextWidget.getTextRowView(title: '${item.nodeName}', value: item.createBy!),
          AppGaps.vGap12,
          _getSignWidget(title: '签字笔迹', signUrl: item.signatureAddress!),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '审批时间', value: item.createDate!),
          AppGaps.vGap12,
          CustomTextWidget.getTextRowView(title: '审批意见', value: item.reviewComments!),
        ],
      ),
    );
  }

  /// 签字显示
  Widget _getSignWidget({required String title, required String signUrl}) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        const Spacer(),
        signUrl.isNotEmpty
            ? Container(
                width: 76.w,
                height: 36.w,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(4.w),
                    color: Colors.white,
                    border: Border.all(
                      color: const Color(0xFFEFF2F6),
                      width: 1,
                    ),
                    image: DecorationImage(
                      image: NetworkImage(Interface.fileUrl + signUrl),
                    )),
              ).clickShowLarge(url: Interface.fileUrl + signUrl)
            : Container(
                width: 76.w,
                height: 36.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(4.w),
                  color: Colors.white,
                  border: Border.all(
                    color: const Color(0xFFEFF2F6),
                    width: 1,
                  ),
                ),
              )
      ],
    );
  }
}
